package com.znyj.servlet.service;


import com.znyj.servlet.dao.TaskDao;
import com.znyj.servlet.entity.TaskEntity;
import com.znyj.servlet.model.TaskQueryModel;
import com.znyj.servlet.sqlHelper.Order;
import com.znyj.servlet.sqlHelper.SqlRequest;
import com.znyj.servlet.vo.Page;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by gaoliwei02 on 17/6/3.
 */
@Service
public class TaskService {
    private static final Logger logger = Logger.getLogger(TaskService.class);

    @Autowired
    private TaskDao taskDao;

//    public int insertCdbTask(CdbTaskEntity cdbTaskEntity) {
//        return cdbTaskDao.insert(cdbTaskEntity);
//    }
//
//    public int updateCdbTask(CdbTaskEntity cdbTaskEntity) {
//        return cdbTaskDao.update(cdbTaskEntity);
//    }
//
//    public int findByOrderIdAndUpdateStatus(String orderId, Timestamp orderFinishTime, int sendStatus) {
//        return cdbTaskDao.findByOrderIdAndUpdateStatus(orderId, orderFinishTime, sendStatus);
//    }
//
//    public long findNewestOrderId() {
//        long orderId;
//        try {
//            orderId = cdbTaskDao.findNewestOrderId();
//        } catch (Exception e) {
//            orderId = 0;
//            logger.error("find orderId null");
//        }
//        return orderId;
//    }

    public TaskEntity findNewestDeviceId(String deviceId) {
        return taskDao.findNewestDeviceId(deviceId);
    }

    public TaskEntity findLast(String deviceId) {
        return taskDao.findLast(deviceId);
    }

    public TaskEntity findByOrderId(Long orderId) {
        return taskDao.findByOrderId(orderId);
    }

    public Page<TaskEntity> createTaskPage(TaskQueryModel taskQueryModel) {
        //int skip = taskQueryModel.//(taskQueryModel.getPageNumber() - 1) * taskQueryModel.getPageSize();
        int skip = taskQueryModel.getOffset();
        SqlRequest sqlRequest = new SqlRequest("cdb_task", "*");

        if (taskQueryModel.getDeviceId() == null || taskQueryModel.getDeviceId().equals("")) {

        } else {
            sqlRequest.EQ("device_id", taskQueryModel.getDeviceId());
        }

        if (taskQueryModel.getOrderId() == -1) {

        } else {
            sqlRequest.EQ("order_id", taskQueryModel.getOrderId());
        }

        if (taskQueryModel.getUserId() == null || taskQueryModel.getUserId().equals("")) {

        } else {
            sqlRequest.EQ("user_id", taskQueryModel.getUserId());
        }

        if (taskQueryModel.getControlMsg() == null || taskQueryModel.getControlMsg().equals("")) {

        } else {
            sqlRequest.EQ("control_msg", taskQueryModel.getControlMsg());
        }

        //sqlRequest.LT("create_time", taskQueryModel.getCreateTime());

        if (taskQueryModel.getOrder().equals("asc")) {
            sqlRequest.addOrder("id", Order.Sort.ASC);
        } else {
            sqlRequest.addOrder("id", Order.Sort.DESC);
        }

        int count = taskDao.count(sqlRequest);
        sqlRequest.Limit(skip, taskQueryModel.getPageSize());
        if (taskQueryModel.getPageSize() == -1) {
            return null;
        }

        List<TaskEntity> taskEntities = taskDao.query(sqlRequest);

        Page<TaskEntity> page = Page.createPage(taskQueryModel.getPageNumber(), taskQueryModel.getPageSize(), taskEntities, count);
        return page;
    }

}
